import config from './config.js'
/**
 * 
 * @param {参数列表} params 
 * @param {URL} url 
 * @param {method}  
 * @param {conyentTpye}  
 */
const appRequest = function(params = {}, url,showError=false,method,contentType="application/x-www-form-urlencoded") {
	let data = params|| {};
	let head = {
		'Content-Type': contentType,
	};
	
	let accessToken = uni.getStorageSync("access_token");
	if (accessToken) {
		head['Authori-zation'] = "Bearer " + accessToken;
	}
	head['appId'] = wx.getAccountInfoSync().miniProgram.appId
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.host + url, //仅为示例，并非真实接口地址。
			data: data,
			header: head,
			method: method,
			dataType: 'json',
			success: (response) => {
				//console.log(response)
				// 未登录拦截处理
				if(response.statusCode === 401) {
						// store.commit("logout");
						uni.showToast({
							title: response.data.msg,
							duration: 2000,
							icon: 'none',
						});
						setTimeout(()=>{
							uni.navigateTo({
								url:'/pages/public/login'
							})
						}, 2000)
						return 
				}
				// 业务失败响应
				if(response.data.code > 0 && showError){
					uni.showToast({
						title: response.data?.msg,
						duration: 2000,
						icon: 'none'
					});
					reject(`[${response.data.code}] ${response.data?.msg}`)
				}
				
				resolve(response.data)
			},
			fail: (err) => {
				console.log('request fail', err);
				if (showError) {
					uni.showToast({
						title: "请求失败",
						showCancel: 2000,
						icon: 'none'
					});
				}
			},
			complete: (re) => {
				//console.log("complete",re)
				if(re.statusCode == 500){
					uni.showToast({
						title: res.data.msg,
						duration: 2000,
						icon: 'none'
					});
					resolve(re.data)
				}
			},
		});
	})
}


/**
 * 
 * @param {参数} data 
 * @param {*} url 
 * @param {*} showError 
 */
export function appGet(data = {}, url,showError,conyentTpye="application/x-www-form-urlencoded") {
	return appRequest(data, url,showError,"GET",conyentTpye);
}


/**
 * 
 * @param {参数} data 
 * @param {*} url 
 * @param {*} conyentTpye 
 */
export function appDelete(data = {}, url, showError,conyentTpye="application/x-www-form-urlencoded") {
	return appRequest(data, url,showError,'DELETE',conyentTpye);
}

/**
 * 
 * @param {参数} data 
 * @param {*} url 
 * @param {*} conyentTpye 
 */
export function appPost(data = {}, url, showError,conyentTpye="application/json") {
	return appRequest(data, url,showError,'POST',conyentTpye);
}

/**
 * 
 * @param {参数} data 
 * @param {*} url 
 * @param {*} deal 
 */
export function appPostObject(data = {}, url, showError,conyentTpye="application/json") {
	return appRequest(JSON.stringify(data), url,showError,'POST',conyentTpye);
}


/**
 * 
 * @param {参数} data 
 * @param {*} url 
 */
export function appPut(data = {}, url, showError, conyentTpye="application/json") {
	return appRequest(JSON.stringify(data), url,showError,'PUT',conyentTpye);
}

/**
 * 上传列表
 * @param {string} image 图片
 */
export function appUpload(image, url) {
	let header = {
	};

	let accessToken = uni.getStorageSync("access_token");
	if (accessToken) {
		header['Authori-zation'] = "Bearer " + accessToken;
	}
	
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: config.host + url, //接口处理在下面有写
			filePath: image,
			name: 'file',
			header: header,
			success: function(response) {
				let resp = JSON.parse(response.data)
				resolve(resp.data.url);
			},
			fail: function(e) {
				console.log('uploadImage fail', e);
				uni.showModal({
					content: "上传失败",
					showCancel: false
				});
			}
		});
	});
}

export default {
	appPost,
	appGet,
	appDelete,
	appPut,
	appUpload
}
